package isquirrel.recommender;

import isquirrel.beans.Property;
import isquirrel.beans.Recommendation;
import isquirrel.beans.Url;
import isquirrel.beans.User;
import isquirrel.semantic.DBpedia;
import isquirrel.util.HibernateUtil;

import java.io.UnsupportedEncodingException;
import java.util.Iterator;
import java.util.List;

import org.junit.Test;

public class DynamicRecommenderTest {
	
	public User createUser1() {
		User u = new User();
		u.setFirstName("August");
		u.setLastName("Kadino");
		
		HibernateUtil.getSession().saveOrUpdate(u);
		return u;
	}
	
	public void addWikipage(User u, String wikipediaURL) {
		DBpedia dbPedia = new DBpedia();
		List<Property> wikiProperties= dbPedia.getPageInformation(wikipediaURL);
		Iterator<Property> wiki_iter = wikiProperties.iterator();
		while (wiki_iter.hasNext()){
			u.addProperty(wiki_iter.next());
		}
	}
	
	public void addTestUrls(User u) {
		String wikipediaURL = "http://en.wikipedia.org/wiki/Michael_moore";
		String wikipediaURL2 = "http://en.wikipedia.org/wiki/George_W._Bush";
		String wikipediaURL3 = "http://en.wikipedia.org/wiki/Bill_Clinton";
		String wikipediaURL4 = "http://en.wikipedia.org/wiki/John_F._Kennedy";
		String wikipediaURL5 = "http://en.wikipedia.org/wiki/William_McKinley";
		
		
		System.out.println("Adding : "+wikipediaURL);
		addWikipage(u, wikipediaURL);
		System.out.println("Adding : "+wikipediaURL2);
		addWikipage(u, wikipediaURL2);
		System.out.println("Adding : "+wikipediaURL3);
		addWikipage(u, wikipediaURL3);
		System.out.println("Adding : "+wikipediaURL4);
		addWikipage(u, wikipediaURL4);
		System.out.println("Adding : "+wikipediaURL5);
		addWikipage(u, wikipediaURL5);
	}

	//Test
	public void testUpdateRecommendations() {
		HibernateUtil.useTestDB();
		HibernateUtil.beginTransaction();
		
		User u = createUser1();
		addTestUrls(u);
		
		System.out.println("Updating user dynamic recommendations");
		DynamicRecommender.updateRecommendations(u);
		
		for (Url url : u.getUrls()) {
			System.out.println(url.getAddress()+ " " + url.getTitle());
		}
		
		System.out.println("Found "+u.getUrls().size() + " recommendations");
		
		HibernateUtil.commit();
	}

	//Test
	public void testGetFrequentProperties() {
		HibernateUtil.useTestDB();
		HibernateUtil.beginTransaction();
		
		User u = createUser1();
		addTestUrls(u);
		
		List<Property> frequent = DynamicRecommender.getFrequentProperties(u, 10);
		
		for (Property p : frequent)
			System.out.println(p.getAttribute()+ " " + p.getValue() + " " + p.getFrequency());
		
		HibernateUtil.commit();
	}
	

	@Test
	public void testGetRecommendations() {
		HibernateUtil.useTestDB();
		HibernateUtil.beginTransaction();
		
		User u = createUser1();
		addTestUrls(u);
		
		
		System.out.println("Updating user dynamic recommendations");
		DynamicRecommender.updateRecommendations(u);
		
		System.out.println("Available recommendations : "+u.getUrls().size());

		for (int k = 0; k < 5; k++) {
			System.out.println("Getting dynamic recommendations ("+(k+1)+")");
			try {
				List<Recommendation> recs = DynamicRecommender.getRecommendations(u, false, 10,false);
				System.out.println("Found "+recs.size()+ " recommendations");
				for (Recommendation r : recs) {
					System.out.println(r.getPageTitle()+ " " + r.getUrl());
				}
				
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		HibernateUtil.commit();
	}


}
